package com.example.neteasecloudmusic.ui.dicovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.discoveryBean.Creatives;
import com.example.neteasecloudmusic.bean.discoveryBean.Image;
import com.example.neteasecloudmusic.ui.PlayListActivity;
import com.example.neteasecloudmusic.view.WequalsHImageView;

import java.util.List;

public class SongMenuAdapter extends RecyclerView.Adapter {

    List<Creatives> creatives;
    private Context context;

    public SongMenuAdapter(Context context, List<Creatives> creatives) {
        this.context = context;
        this.creatives = creatives;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.menu_item, parent, false);
        return new MyHolder(view);
    }

    /**
     * TODO: waiting optim
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder = (MyHolder) holder;
        myHolder.textView.setText(creatives.get(position).getUiElement().getMainTitle().getTitle());
        Glide.with(holder.itemView)
                .load(creatives.get(position).getUiElement().getImage().getImageUrl())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(myHolder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context.getApplicationContext(), PlayListActivity.class);
                intent.putExtra(PlayListActivity.PLAYLIST_ID, "11");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return creatives.size();
    }

    private class MyHolder extends RecyclerView.ViewHolder {
        WequalsHImageView imageView;
        TextView textView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.menuItemImage);
            textView = itemView.findViewById(R.id.menuImageDetail);
        }
    }
}
